//
//  DTCDataSaveToFileManager.m
//  SmartYunYiWindow
//
//  Created by JianWei Chen on 17/3/25.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "DTCDataSaveToFileManager.h"

@interface DTCDataSaveToFileManager ()

@property (nonatomic, strong) NSMutableDictionary *valueMDic;//数据
@property (nonatomic, strong) NSString *filePath;

@end

@implementation DTCDataSaveToFileManager

+(DTCDataSaveToFileManager *)standardUserDefaults
{
    static DTCDataSaveToFileManager *st_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        st_manager = [[DTCDataSaveToFileManager alloc]init];
    });
    return st_manager;
}

- (instancetype)init
{
    if ([super init]) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:self.filePath];
        if (dic == nil) {
            [@{} writeToFile:self.filePath atomically:YES];
        }
    }
    return self;
}
- (NSMutableDictionary *)valueMDic
{
    if (!_valueMDic) {
        _valueMDic = [[NSMutableDictionary alloc]initWithContentsOfFile:self.filePath];
    }
    return _valueMDic;
}

- (NSString *)filePath
{
    if (!_filePath) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //获取完整路径
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"DTSystemSaveData.plist"];//这里就是你将要存储的沙盒路径
        _filePath = plistPath;
    }
    return _filePath;
}

- (void)setValue:(id)value forKey:(NSString *)key
{
    [self.valueMDic setValue:value forKey:key];
    [self.valueMDic writeToFile:self.filePath atomically:YES];
}

- (id)valueForKey:(NSString *)key
{
    return [self.valueMDic valueForKey:key];
}

- (void)removeObjectForKey:(NSString *)key
{
    [self.valueMDic removeObjectForKey:key];
    [self.valueMDic writeToFile:self.filePath atomically:YES];
}

@end
